package com.xpgk.mvc.scallVisit;

import java.math.BigDecimal;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.jfinal.aop.Before;
import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.Record;
import com.jfinal.plugin.activerecord.tx.Tx;
import com.xpgk.constant.ConstantSplitPage;
import com.xpgk.dto.RenderBean;
import com.xpgk.dto.SplitPage;
import com.xpgk.mvc.base.BaseController;
import com.xpgk.mvc.bcallVisit.BcallVisit;
import com.xpgk.mvc.bcallVisit.BcallVisitService;
import com.xpgk.mvc.bentryRegister.Customer;
import com.xpgk.mvc.bentryRegister.CustomerAgreement;
import com.xpgk.mvc.bentryRegister.CustomerDetail;
import com.xpgk.mvc.measurement.Temporary;
import com.xpgk.mvc.organization.Organization;
import com.xpgk.mvc.statistic.daySales.DaySalesXLS;

public class ScallVisitController extends BaseController {
	
	private ScallVisitService scallVisitService = new ScallVisitService();
	
	/*
	 * 电话回访查询跳转
	 */
	public void index(){
		setAttr("curOrgan", Organization.dao.findById(getCUserOrganIds()));
		render("scallVisit.html");
	}
	/*
	 * 电话回访条件查询
	 */
	public void queryByCustom(){
		String json = paging(splitPage, scallVisitSQL.scallVisit_splitPageSelect, scallVisitSQL.scallVisit_splitPageFrom);		
		renderJson(json);
	}
	/*
	 * 回访信息查询
	 */
	public void queryByCallVisit(){
		String phone = getPara("ids");
		setAttr("phone",phone);
		render("scallVisit3.html");
	}
	
	//电话回访客户信息保存
	@Before(Tx.class)
	public void saveVisitCustomer(){ 
		String  organids=getCUserOrganIds();
		String  cudid=getCUserOrganIds();
		ScallVisit scallVisit = getModel(ScallVisit.class);
		Temporary t = Temporary.dao.findById(scallVisit.getStr(ScallVisit.column_customerids));
		if(t==null){
			scallVisit.set(ScallVisit.column_gkfl,"607f10b77f4d4b908104e990f366a334");
			//scallVisit.set(ScallVisit.column_isSatisfaction, t.get("gkfy"));
			scallVisit.set("organIds", organids);
		}else{
			
		
		scallVisit.set(ScallVisit.column_gkfl, t.get("gkfl"));
		scallVisit.set(ScallVisit.column_isSatisfaction, t.get("gkfy"));
		scallVisit.set("organIds", organids);
		}
		RenderBean result=scallVisitService.saveCallVisit(scallVisit, cudid);
		renderJson(result);
	}
	
	//回访预览（弹窗）
	public  void showscallVisit(){	
		String ids = getPara("customerIds");
		String orgid = getCUserOrganIds();
		List<Record>  username = Db.find("SELECT ids,`name` FROM sys_user WHERE organizationids=? AND `status`=1",orgid);
//		Customer  cus=Customer.dao.findById(phone);
		setAttr("ids", ids);
		setAttr("username", username);
		render("scallVisit2.html");
	}
	
	//回访信息查询（测算顾客查询）
	public void showscallVisit2(){
		String json = paging(splitPage, scallVisitSQL.scallVisit_splitPageSelectvisit, scallVisitSQL.scallVisit_splitPageFromvisit);
		renderJson(json);
	}
	
	//回访信息查询（成交顾客查询）
	public void showscallVisit3(){
			String json = paging(splitPage, scallVisitSQL.scallVisit_splitPageSelectvisit2, scallVisitSQL.scallVisit_splitPageFromvisit2);
			renderJson(json);
		}

	/*
	 * 查询结果导出
	 */
	public void excel(){
		String organIdsEmployee = getPara("organIdsEmployee");
		String name = getPara("name");
		String phone = getPara("phone");
		String arrivedMinDate = getPara("arrivedMinDate");
		String arrivedMaxDate = getPara("arrivedMaxDate");
		String minResultLostWeight = getPara("minResultLostWeight");
		String maxResultLostWeight = getPara("maxResultLostWeight");
		String arrivedMinTimes = getPara("arrivedMinTimes");
		String arrivedMaxTimes = getPara("arrivedMaxTimes");
		String minLostWeightAmount = getPara("minLostWeightAmount");
		String maxLostWeightAmount = getPara("maxLostWeightAmount");
		String isSatisfaction = getPara("isSatisfaction");
		Organization organ = Organization.dao.findById(organIdsEmployee);//门店
		String search_organIdsEmployee =organ.getStr(Organization.column_name).equals("")?"":("店面名称："+organ.getStr(Organization.column_name)+"，");
		String search_name = name.equals("")?"":(("姓名：")+ name+"，" );
		String search_phone = getPara("phone").equals("")?"":(("手机号：")+phone+"，");
		String search_arrivedMinDate = getPara("arrivedMinDate").equals("")?"":(("最早到店日期：")+arrivedMinDate+"，");
		String search_arrivedMaxDate = getPara("arrivedMaxDate").equals("")?"":(("最晚到店日期：")+arrivedMaxDate+"，");
		String search_minResultLostWeight = getPara("minResultLostWeight").equals("")?"":(("最小减重效果：")+minResultLostWeight+"，");
		String search_maxResultLostWeight = getPara("maxResultLostWeight").equals("")?"":(("最大减重效果：")+maxResultLostWeight+"，");
		String search_arrivedMinTimes = getPara("arrivedMinTimes").equals("")?"":(("最少到店次数：")+arrivedMinTimes+"，");
		String search_arrivedMaxTimes = getPara("arrivedMaxTimes").equals("")?"":(("最多到店次数：")+arrivedMaxTimes+"，");
		String search_minLostWeightAmount = getPara("minLostWeightAmount").equals("")?"":(("最小减重斤数：")+minLostWeightAmount+"，");
		String search_maxLostWeightAmount = getPara("maxLostWeightAmount").equals("")?"":(("最大减重斤数：")+maxLostWeightAmount+"，");
		String search_isSatisfaction = "";
		if(isSatisfaction!=""){
			if(isSatisfaction.equals("1")){
				search_isSatisfaction="满意";
			}else if(isSatisfaction.equals("2")){
				search_isSatisfaction="一般";
			}else if(isSatisfaction.equals("3")){
				search_isSatisfaction="差";
			}
		}
		search_isSatisfaction = search_isSatisfaction.equals("")?"":("满意度："+search_isSatisfaction+"，");
		String search = search_organIdsEmployee+search_name+search_phone+search_arrivedMinDate+search_arrivedMaxDate+search_minResultLostWeight+search_maxResultLostWeight+search_arrivedMinTimes+search_arrivedMaxTimes+search_minLostWeightAmount+search_maxLostWeightAmount+search_isSatisfaction;
		SplitPage splitPage2 = new SplitPage();
		Map<String, Object> param = new HashMap<String, Object>();
		param.put("organIdsEmployee",organIdsEmployee );
		param.put("name",name );
		param.put("phone",phone );
		param.put("minResultLostWeight",minResultLostWeight );
		param.put("maxResultLostWeight",maxResultLostWeight );
		param.put("arrivedMinDate",arrivedMinDate );
		param.put("arrivedMaxDate",arrivedMaxDate );
		param.put("arrivedMinTimes",arrivedMinTimes );
		param.put("arrivedMaxTimes",arrivedMaxTimes );
		param.put("minLostWeightAmount",minLostWeightAmount );
		param.put("maxLostWeightAmount",maxLostWeightAmount );
		param.put("isSatisfaction",isSatisfaction );
		splitPage2.setQueryParam(param);
		splitPage2.setPageSize(ConstantSplitPage.default_maxSize);
		String json = paging(splitPage2, scallVisitSQL.scallVisit_splitPageSelect, scallVisitSQL.scallVisit_splitPageFrom);
		List<Record> list = (List<Record>)splitPage2.getList();
		for(Record r : list){
			String issatisfaction = r.getStr("issatisfaction");
			if(issatisfaction!=null){
				if(issatisfaction.equals("1")){
					r.set("issatisfaction","满意");
				}else if(issatisfaction.equals("2")){
					r.set("issatisfaction","一般");
				}else if(issatisfaction.equals("3")){
					r.set("issatisfaction", "差");
				}
			}
			double nowweight = Double.parseDouble(r.get("nowweight").toString());
			double weight = Double.parseDouble(r.get("firstweight").toString());

			double reducedWeight =weight-nowweight;
			r.set("reducedWeight", reducedWeight);
			double delweight = Double.parseDouble(r.get("delweight").toString());
			double resultWeight2 = reducedWeight*100/delweight;
			BigDecimal bg = new BigDecimal(resultWeight2);  
            double resultWeight = bg.setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue();
			r.set("resultWeight", resultWeight);
		}
		String str = "1";
		render(new ScallVisitXLS((List<Record>)splitPage2.getList(), "电话回访查询导出",search));
	}
	
}
